package com.ttai.presenter.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Parcelable;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ttai.model.dao.DBHelper;
import com.ttai.model.net.ResponseInfo;
import com.ttai.model.net.TokenInfo;
import com.ttai.model.net.VerifyAccountBean;
import com.ttai.model.net.VerifyIdBean;
import com.ttai.presenter.base.AllBasePresenter;
import com.ttai.ui.activity.EnterPage;
import com.ttai.ui.activity.UserHomePage;
import com.ttai.ui.activity.VerifyLogin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

public class LoginPresenter extends AllBasePresenter{
    EnterPage enterPage;

    public String myToken="";
    public static String newAcountValue="";
    public static String tipAcountValue="";
    public static String idAcountValue="";
    private int flag;
    private String loginOrotp;

    public LoginPresenter(EnterPage enterPage) {
        this.enterPage = enterPage;
    }

    @Override
    protected void showError(String message) {
        Toast.makeText(enterPage,message,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void parseJsonDataCode(Integer code, JsonObject json) {
        Gson gson = new Gson();
        if (code==200){
            Toast.makeText(enterPage,"登录成功",Toast.LENGTH_SHORT).show();
            TokenInfo tokenInfo = gson.fromJson(json,TokenInfo.class);
            insertToken(tokenInfo.getToken());
//            recoverDB();
            enterPage.finish();
            Intent intent=new Intent();
            intent.setClass(enterPage,UserHomePage.class);
            enterPage.startActivity(intent);
        }if (code==203){
            VerifyIdBean verifyIdBean = gson.fromJson(json,VerifyIdBean.class);
            List<VerifyIdBean.SellectListBean> idList = new ArrayList<>();
            idList = verifyIdBean.getSellectList();
            flag = verifyIdBean.getFlag();
//            loginOrotp = verifyIdBean.getLoginOrotp();
            enterPage.finish();
            Intent intent = new Intent(enterPage,VerifyLogin.class);
            intent.putExtra("index","1");
            intent.putExtra("flag",String.valueOf(flag));
//            intent.putExtra("loginOrotp",loginOrotp);
            intent.putExtra("idList", (Serializable) idList);
            enterPage.startActivity(intent);

        }if (code==202){
            VerifyAccountBean verifyAccountBean = gson.fromJson(json,VerifyAccountBean.class);
            List<VerifyAccountBean.SellectListBean> accountList = new ArrayList<>();
            accountList = verifyAccountBean.getSellectList();
            flag = verifyAccountBean.getFlag();
//            loginOrotp = verifyAccountBean.getLoginOrotp();
            enterPage.finish();
            Intent intent = new Intent(enterPage,VerifyLogin.class);
            intent.putExtra("index","0");
            intent.putExtra("flag",String.valueOf(flag));
//            intent.putExtra("loginOrotp",loginOrotp);
            intent.putExtra("accoutnList", (Serializable) accountList);
            enterPage.startActivity(intent);
        }
    }


    private void insertToken(String token) {
        DBHelper dbHelper = DBHelper.getInstance(enterPage);
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("token",token);
        db.update("tokenacount",values, null, null);
    }

    public void getLoginInfo(String phone,String scode,String cid){
        Call<ResponseInfo> loginInfo = responseInfoApi.login(phone,scode,cid);
        loginInfo.enqueue(new CallBackAllAdapter());
    }
}
